/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;

import logica.ManejadorRestaurante;
import org.primefaces.event.map.OverlaySelectEvent;
import org.primefaces.model.map.Circle;
import org.primefaces.model.map.DefaultMapModel;
import org.primefaces.model.map.LatLng;
import org.primefaces.model.map.MapModel;
import org.primefaces.model.map.Marker;

/**
 *
 * @author Jose
 */
@ManagedBean
@ViewScoped
public class MapaGPS implements Serializable {

    private MapModel advancedModel;
    int radio_busqueda=100;
    double latitud_us;
   double longitud_us;
    private Marker marker;
    ArrayList<String> match_markers;

    /**
     * Creates a new instance of Mapa
     */
    public MapaGPS() {
        match_markers=new  ArrayList<String>();
        
        //14.587853, -90.553014 t-3
        latitud_us=14.587853;
        longitud_us=-90.553014;
    }

    public LatLng getCoordenaMiUbicacion() {
        return new LatLng(latitud_us, longitud_us);
    }

    @PostConstruct
    public void init() {

        advancedModel = new DefaultMapModel();
        ManejadorRestaurante mr = new ManejadorRestaurante();
        List<logica.Restaurante> lista = mr.getAllRestaurante();
        //Shared coordinates

        for (logica.Restaurante res : lista) {
            String[] data_marker = {res.getDescripcion(), res.getFoto(), String.valueOf(res.getCodigo())};
             getMatch_markers().add(res.getNombre()+" - 10m aprox");
            advancedModel.addOverlay(new Marker(res.getCoordena(), res.getNombre(), data_marker, res.getIcon()));
        }
//usuario
          advancedModel.addOverlay(new Marker(this.getCoordenaMiUbicacion(), "Mi",new String[]{"Ubicacion Actual","yo.jpg","0"}, "http://k32.kn3.net/4/8/5/1/8/0/093.png"));
        //circulo
              
        LatLng us = this.getCoordenaMiUbicacion();
 
        //Circle
        Circle circle1 = new Circle(us,this.radio_busqueda);
        circle1.setStrokeColor("#d93c3c");
        circle1.setFillColor("#d93c3c");
        circle1.setFillOpacity(0.4);
 
 
         advancedModel.addOverlay(circle1);
     
    }

    public MapModel getAdvancedModel() {
        return advancedModel;
    }

    public void onMarkerSelect(OverlaySelectEvent event) {
        Object x=event.getOverlay();
        if(x instanceof Marker)
        marker = (Marker) event.getOverlay();
    }

    public Marker getMarker() {
        return marker;
    }

    /**
     * @return the radio_busqueda
     */
    public int getRadio_busqueda() {
        return radio_busqueda;
    }

    /**
     * @param radio_busqueda the radio_busqueda to set
     */
    public void setRadio_busqueda(int radio_busqueda) {
        this.radio_busqueda = radio_busqueda+100;
           if(this.radio_busqueda>0){
               init();
      }
    }

    /**
     * @return the longitud_us
     */
    public double getLongitud_us() {
        return longitud_us;
    }

    /**
     * @param longitud_us the longitud_us to set
     */
    public void setLongitud_us(double longitud_us) {
        this.longitud_us = longitud_us;
    }

    /**
     * @return the match_markers
     */
    public ArrayList<String> getMatch_markers() {
        return match_markers;
    }

    /**
     * @param match_markers the match_markers to set
     */
    public void setMatch_markers(ArrayList<String> match_markers) {
        this.match_markers = match_markers;
    }
}
